package goutil

import (
	"math"
	"os"
	"time"
)

var (
	HomeDir    string
	CurrentDir string
	ConfDir    string
	BqConfDir  string
)

func init() {
	HomeDir, _ = os.UserHomeDir()
	CurrentDir, _ = os.Getwd()
	ConfDir = HomeDir + "/.config"
	BqConfDir = HomeDir + "/.config/bq"
}

func CheckPanicErr(err error) {
	if err != nil {
		panic(err)
	}
}

func CheckMaxFloat64(f float64) (r float64) {
	r = f
	if f == math.MaxFloat64 {
		r = 0
	}
	return
}

func ValideTradeTime(tm string) bool {
	//tm: yyyy-mm-dd hh:mm:ss.ms , 由 time.Now().String() 得到
	//vtm 交易时间区间
	vtm := [][]string{{"08:59", "10:15"}, {"10:30", "11:30"}, {"13:30", "15:00"}, {"20:59", "25:00"}, {"00:00", "02:30"}}
	b := false
	hhmm := tm[11:16]
	for _, v := range vtm {
		if v[0] <= hhmm && hhmm <= v[1] {
			b = true
			break
		}
	}

	return b
}

func ValideTradeUpdateTime(tm string) bool {
	//tm: hh:mm:ss , 由 tick.UpdateTime 得到
	//vtm 交易时间区间
	// fmt.Println(tm)
	vtm := [][]string{{"05:59:00", "10:15:00"}, {"10:30:00", "11:30:00"}, {"13:30:00", "15:15:00"}, {"20:59:00", "25:00:00"}, {"00:00:00", "02:30:00"}}
	b := false

	for _, v := range vtm {
		if v[0] <= tm && tm <= v[1] {
			b = true
			break
		}
	}

	return b
}
func NowStr(format string) string {
	if format == "" {
		format = "20060102 15:05:06"
	}
	return time.Now().Format(format)
}

// 根据可用资金，价格，乘数和保证金率，计算最大可开仓手数
// multiplier,乘数，一手表的所包括的单位
// margin:  保证金率，一百分比正常是10左右
func CalcCanOpenVol(money, price, multiplier, margin float64) (vol int) {
	vol = int(money / (price * multiplier) / margin * 100)
	return

}
